package main

import "fmt"

type Country struct {
	Name string
}

type City struct {
	Name string
}

type Printable interface {
	PrintStr()
}

func (c Country) PrintStr() {
	fmt.Println(c.Name)
}

func (c City) PrintStr() {
	fmt.Println(c.Name)
}

//嵌套结构体
type WithName struct {
	Name string
}

type Country1 struct {
	WithName
}

type City1 struct {
	WithName
}

type Printable1 interface {
	PrintStr1()
}
func (w WithName) PrintStr1() {
	fmt.Println(w.Name)
}


type Stringable interface {
	ToString() string
}

func (c Country) ToString() string {
	return "Country = " + c.Name
}

func (c City) ToString() string {
	return "City = " + c.Name
}

func PrintStr(p Stringable) {
	fmt.Println(p.ToString())
}

//检查接口完整性
type Shape interface {
	Sides() int
	Area() int
}
type Square struct {
	len int
}

func (s* Square) Sides() int {
	return 4
}

func main() {
	c1 := Country{"China"}
	c2 := City{"Beijing"}
	c1.PrintStr()
	c2.PrintStr()

	//嵌套结构体解决重复实现接口函数,嵌套结构体实现接口函数
	c11 := Country1{WithName{"China"}}
	c22 := City1{WithName{"Beijing"}}
	c11.PrintStr1()
	c22.PrintStr1()

	//使用Stringable接口把"业务类型"Country,City的"控制逻辑"Print()解耦,只要实现Stringable接口,都可以传给PrintStr()来使用
	d1 := Country{"USA"}
	d2 := City{"Los Angeles"}
	PrintStr(d1)
	PrintStr(d2)

	s := Square{len: 5}
	fmt.Printf("%d\n", s.Sides())

	//检查接口实现完整性
	//var _ Shape = (*Square)(nil)

}
